<template>
  <section class="store-locator full-width-page">
    <!-- 左侧门店列表 -->
    <aside class="sidebar">
      <input v-model="searchQuery" placeholder="搜索城市" class="search-input" />

      <ul class="store-list">
        <li v-for="store in filteredStores" :key="store.StoreId" :class="{ active: activeStore?.StoreId === store.StoreId }"
          @click="selectStore(store)">
          <h4>{{ store.StoreName }}</h4>
          <p>{{ store.Address }}</p>
          <p class="hours">营业时间：{{ store.details?.OpenHours }}</p>

          <div class="tags">
            <span v-for="(f, i) in store.details?.Features?.split(',')" :key="i" class="tag">
              {{ f }}
            </span>
          </div>
        </li>
      </ul>
    </aside>

    <!-- 右侧地图 -->
    <div id="map" class="map"></div>
  </section>
</template>

<script setup>
import { ref, computed, onMounted } from 'vue'
import L from 'leaflet'
import 'leaflet/dist/leaflet.css'
import http from '@/utils/http'

const stores = ref([])

const searchQuery = ref('')
const activeStore = ref(null)
let map

onMounted(async () => {
  try {
    // 1. 加载门店数据
    const res = await http.get('/stores/')
    stores.value = res.data

    // ✅ 确保有门店数据
    if (stores.value.length === 0) {
      console.warn('暂无门店数据')
      return
    }

    const firstStore = stores.value[0]


    const lat = parseFloat(firstStore.Latitude)
    const lng = parseFloat(firstStore.Longitude)

    map = L.map('map').setView(
      [lat, lng],
      14,
      { animate: true }
    )

    L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
      attribution: '© OpenStreetMap contributors'
    }).addTo(map)

    // 3. 添加门店标记
    stores.value.forEach(store => {
      const marker = L.marker([parseFloat(store.Latitude),
      parseFloat(store.Longitude)])
        .addTo(map)
        .bindPopup(`<b>${store.StoreName}</b><br>${store.Address}`)
        .on('click', () => {
          selectStore(store)
        })
    })
  } catch (err) {
    console.error('初始化门店页面失败：', err)
  }
})

// 选择门店并聚焦地图
function selectStore(store) {
  // console.log(stores)
  activeStore.value = stores.value
  map.setView(
    [parseFloat(store.Latitude), parseFloat(store.Longitude)],
    14,
    { animate: true }
  )
}

// 根据搜索过滤城市
const filteredStores = computed(() => {
  // console.log(stores.value)
  if (!searchQuery.value) return stores.value
  return stores.value.filter(s => s.City?.toLowerCase().includes(searchQuery.value.toLowerCase()))
})
</script>

<style scoped>
.store-locator {
  display: flex;
  height: 600px;
  padding: 40px;
  gap: 24px;
  overflow: hidden;
}

/* 左侧 */
.sidebar {
  width: 340px;
  flex-shrink: 0;
  overflow-y: auto;
  max-height: 100%;
  padding-right: 8px;
}

.search-input {
  width: 100%;
  padding: 10px 14px;
  border: 1px solid #ccc;
  border-radius: 6px;
  font-size: 14px;
  margin-bottom: 16px;
}

.store-list {
  list-style: none;
  padding: 0;
}

.store-list li {
  padding: 14px;
  margin-bottom: 12px;
  border: 1px solid #eee;
  border-radius: 8px;
  cursor: pointer;
  transition: all 0.3s ease;
}

.store-list li:hover,
.store-list li.active {
  border-color: #c19a6b;
  background: #fff8f1;
}

.store-list h4 {
  margin: 0 0 4px;
  font-size: 16px;
  font-weight: 600;
  color: #4a2e18;
}

.store-list p {
  margin: 2px 0;
  font-size: 13px;
  color: #555;
}

.hours {
  font-size: 12px;
}

.tags {
  margin-top: 6px;
}

.tag {
  display: inline-block;
  background: #f1ece7;
  color: #7b5c3c;
  padding: 2px 8px;
  font-size: 12px;
  border-radius: 10px;
  margin-right: 6px;
}

/* 右侧地图 */
.map {
  flex: 1;
  height: 640px;
  margin: 0 50px;
  border-radius: 12px;
  box-shadow: 0 4px 14px rgba(0, 0, 0, 0.08);
}
</style>
